package com.atguigu.day02.transform;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Flink03_FlatMap {
    public static void main(String[] args) {
         Configuration conf = new Configuration();
          //配置web页面固定端口
          conf.setInteger("rest.port", 20000);
          //创建流环境
          StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
          //设置并行度
          env.setParallelism(2);

        DataStreamSource<Integer> stream = env.fromElements(1, 2, 3, 5, 7, 8, 9);
        //flatMap:1->N  一进多出,N可以为0，1...  可以替换map和filter。
        stream.flatMap(new FlatMapFunction<Integer, Integer>() {
            @Override
            public void flatMap(Integer value, Collector<Integer> out) throws Exception {
                //可以输出平方和立方，一进多出
                out.collect(value * value);
                out.collect(value * value * value);
            }
        }).print();

        try {
              env.execute();
          } catch (Exception e) {
              e.printStackTrace();
          }
    }
}
